package net.yzjlb.sai.mrp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class MrpUnpack {

	byte[] data;
	public MrpConfig config;

	public MrpUnpack(byte[] data) {
		this.data = data;
		config = new MrpConfig();
		updateConfig();
	}

	public MrpUnpack(File file) {
		FileInputStream input;
		config = new MrpConfig();
		if(!file.isFile()){
			System.err.println("文件不存在");
		}
		try {
			input = new FileInputStream(file);
			int len = input.available();
			byte[] data = new byte[len];
			input.read(data);
			input.close();
			this.data = data;
			updateConfig();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void updateConfig() {
		config.FileStart = readInt(data, 4);
		config.MrpTotalLen = readInt(data, 8);
		config.MRPHeaderSize = readInt(data, 12);
		config.FileName = readGBKString(data, 16);
		config.DisplayName = readGBKString(data, 28);
		config.AuthStr = readGBKString(data, 52);
		config.Appid = readInt(data, 68);
		config.Version = readInt(data, 72);
		config.Flag = readInt(data, 76);
		config.BuilderVersion = readInt(data, 80);
		config.Crc32 = readInt(data, 84);
		config.Vendor = readGBKString(data, 88);
		config.Desc = readGBKString(data, 128);
		config.AppidBE = readBigInt(data, 192);
		config.VersionBE = readBigInt(data, 196);
		config.Reserve2 = readInt(data, 200);
		config.ScreenWidth = readInt(data, 204);
		config.ScreenHeight = readInt(data, 206);
		config.Plat = data[208];
		System.out.println("文件名:"+config.FileName);
		System.out.println("显示名:"+config.DisplayName);
		System.out.println("appid:"+config.Appid);
		System.out.println("BuildVersion:"+config.BuilderVersion);
	}

	//解包
	public void unpack(String outDir){
		File file_outDir = new File(outDir);
		int listStart = config.MRPHeaderSize;
		int listEnd = config.FileStart + 8;
		for(int offset = listStart;offset<listEnd;){
			//文件名长度
			int fileNameLen = readInt(data, offset);
			offset+=4;
			String filename = readGBKString(data, offset);
			System.out.println("filename:"+filename);
			offset+=fileNameLen;
			int fileOffset = readInt(data, offset);
			offset+=4;
			int fileLen = readInt(data, offset);
			offset+=4;
			offset+=4;
			FileOutputStream outputStream;
			try {
				outputStream = new FileOutputStream(new File(file_outDir,filename));
				byte[] temp_data = new byte[fileLen];
				System.arraycopy(data, fileOffset, temp_data, 0, fileLen);
				outputStream.write(temp_data);
				outputStream.close();
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private int readInt(byte[] temp, int offset){
		int num = 0;
		num|= 0xff & temp[offset];
		num|= (0xff & temp[offset+1])<<8;
		num|= (0xff & temp[offset+2])<<16;
		num|= (0xff & temp[offset+3])<<24;
		return num;
	}

	private int readBigInt(byte[] temp, int offset){
		int num = 0;
		num|= 0xff & temp[offset+3];
		num|= (0xff & temp[offset+2])<<8;
		num|= (0xff & temp[offset+1])<<16;
		num|= (0xff & temp[offset])<<24;
		return num;
	}

	private String readGBKString(byte[] temp, int offset){
		int len = 0;
		for(int i=offset;i<temp.length;i++){
			if(temp[i]!=0)
				len++;
			else break;
		}

		try {
			byte[] bytes = new byte[len];
			System.arraycopy(temp, offset, bytes, 0, len);
			return new String(bytes,"GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
